clear all 
cd "~\Data\Open_Close_Auctions"

use auction_price_impact2021.dta, clear 

keep if year(date)>2011

merge 1:1 permno date using merged_permno_date_from_step2_sample_noAmex.dta

keep if _merge==3
 
drop _merge
duplicates drop symbol date, force 


merge 1:1 symbol date using price_intraday
drop if _merge==2


sort permno date
xtset permno date

gen pct_oi=(close_imb_qty)/adv_10d
gen signed_pct_oi1=pct_oi  if inlist(close_imb_side, "B")
replace signed_pct_oi1=-1*pct_oi if close_imb_side=="S"
replace signed_pct_oi1=0 if missing(signed_pct_oi1)

* change the treatment of outliers where the imbalance is beyond 10% of the ADV
replace signed_pct_oi1=. if signed_pct_oi1>0.1
replace signed_pct_oi1=. if signed_pct_oi1<-0.1
gen closing_auction_oib=signed_pct_oi1


gen signed_pct_open_oi=imbalance/adv_10d if inlist(imbalance_side, "B")
replace signed_pct_open_oi=-1*imbalance/adv_10d if inlist(imbalance_side, "S")
replace signed_pct_open_oi=0 if missing(signed_pct_open_oi)

replace signed_pct_open_oi=. if signed_pct_open_oi>0.1
replace signed_pct_open_oi=. if signed_pct_open_oi<-0.1
gen open_auction_oib=signed_pct_open_oi

gen openinterest_count=imbalance+paired
gen ocount=1 if !missing(openinterest_count) & openinterest_count!=0


 
gen subsample_group=1 if   !missing(lag1_me) & lag1_me > me_cutoff50
replace subsample_group=2 if missing(subsample_group) & !missing(lag1_me) & lag1_me < me_cutoff50 & lag1_me >= me_cutoff20
replace subsample_group=3 if missing(subsample_group) & !missing(lag1_me) & lag1_me < me_cutoff20

gen DummySmall=1 if subsample_group==2
gen DummyLarge=1 if subsample_group==1
gen DummyMicro=1 if subsample_group==3
replace DummySmall=0 if missing(DummySmall)
replace DummyLarge=0 if missing(DummyLarge)
replace DummyMicro=0 if missing(DummyMicro)


capture drop ret_closing_auction


* closing auction return 
gen ret_closing_auction=(abs(prc)-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_closing_auction=(abs(prc)-price_1550)/price_1550 if date>=mdy(4,1,2019)


replace ret_closing_auction=. if ret_closing_auction>0.10 & !missing(ret_closing_auction)
replace ret_closing_auction=. if ret_closing_auction<-0.10 & !missing(ret_closing_auction)
replace ret_closing_auction=ret_closing_auction*1
replace ret_closing_auction=. if missing(prc) | missing(price1545)


gen ret_CI_plus_1min=(price_1546-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_1min=(price_1551-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_plus_1min=. if ret_CI_plus_1min>0.10 & !missing(ret_CI_plus_1min)
replace ret_CI_plus_1min=. if ret_CI_plus_1min<-0.10 & !missing(ret_CI_plus_1min)
replace ret_CI_plus_1min=ret_CI_plus_1min*100
replace ret_CI_plus_1min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1546) )
replace ret_CI_plus_1min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1551))


gen ret_CI_plus_2min=(price_1547-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_2min=(price_1552-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_plus_2min=. if ret_CI_plus_2min>0.10 & !missing(ret_CI_plus_2min)
replace ret_CI_plus_2min=. if ret_CI_plus_2min<-0.10 & !missing(ret_CI_plus_2min)
replace ret_CI_plus_2min=ret_CI_plus_2min*100
replace ret_CI_plus_2min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1547) )
replace ret_CI_plus_2min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1552))


gen ret_CI_plus_3min=(price_1548-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_3min=(price_1553-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_plus_3min=. if ret_CI_plus_3min>0.10 & !missing(ret_CI_plus_3min)
replace ret_CI_plus_3min=. if ret_CI_plus_3min<-0.10 & !missing(ret_CI_plus_3min)
replace ret_CI_plus_3min=ret_CI_plus_3min*100
replace ret_CI_plus_3min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1548) )
replace ret_CI_plus_3min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1553))


gen ret_CI_plus_4min=(price_1549-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_4min=(price_1554-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_plus_4min=. if ret_CI_plus_4min>0.10 & !missing(ret_CI_plus_4min)
replace ret_CI_plus_4min=. if ret_CI_plus_4min<-0.10 & !missing(ret_CI_plus_4min)
replace ret_CI_plus_4min=ret_CI_plus_4min*100
replace ret_CI_plus_4min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1549) )
replace ret_CI_plus_4min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1554))


gen ret_CI_plus_5min=(price_1550-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_5min=(price_1555-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_plus_5min=. if ret_CI_plus_5min>0.10 & !missing(ret_CI_plus_5min)
replace ret_CI_plus_5min=. if ret_CI_plus_5min<-0.10 & !missing(ret_CI_plus_5min)
replace ret_CI_plus_5min=ret_CI_plus_5min*100
replace ret_CI_plus_5min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1550) )
replace ret_CI_plus_5min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1555))


gen ret_CI_plus_6min=(price_1551-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_6min=(price_1556-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_plus_6min=. if ret_CI_plus_6min>0.10 & !missing(ret_CI_plus_6min)
replace ret_CI_plus_6min=. if ret_CI_plus_6min<-0.10 & !missing(ret_CI_plus_6min)
replace ret_CI_plus_6min=ret_CI_plus_6min*100
replace ret_CI_plus_6min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1551) )
replace ret_CI_plus_6min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1556))


gen ret_CI_plus_7min=(price_1552-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_7min=(price_1557-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_plus_7min=. if ret_CI_plus_7min>0.10 & !missing(ret_CI_plus_7min)
replace ret_CI_plus_7min=. if ret_CI_plus_7min<-0.10 & !missing(ret_CI_plus_7min)
replace ret_CI_plus_7min=ret_CI_plus_7min*100
replace ret_CI_plus_7min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1552))
replace ret_CI_plus_7min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1557))


gen ret_CI_plus_8min=(price_1553-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_8min=(price_1558-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_plus_8min=. if ret_CI_plus_8min>0.10 & !missing(ret_CI_plus_8min)
replace ret_CI_plus_8min=. if ret_CI_plus_8min<-0.10 & !missing(ret_CI_plus_8min)
replace ret_CI_plus_8min=ret_CI_plus_8min*100
replace ret_CI_plus_8min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1553) )
replace ret_CI_plus_8min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1558))


gen ret_CI_plus_9min=(price_1554-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_9min=(price_1559-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_plus_9min=. if ret_CI_plus_9min>0.10 & !missing(ret_CI_plus_9min)
replace ret_CI_plus_9min=. if ret_CI_plus_9min<-0.10 & !missing(ret_CI_plus_9min)
replace ret_CI_plus_9min=ret_CI_plus_9min*100
replace ret_CI_plus_9min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1554) )
replace ret_CI_plus_9min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1559))


gen ret_CI_plus_10min=(price_1555-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_10min=(abs(prc)-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_plus_10min=. if ret_CI_plus_10min>0.10 & !missing(ret_CI_plus_10min)
replace ret_CI_plus_10min=. if ret_CI_plus_10min<-0.10 & !missing(ret_CI_plus_10min)
replace ret_CI_plus_10min=ret_CI_plus_10min*100
replace ret_CI_plus_10min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1555) )
replace ret_CI_plus_10min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(prc))


gen ret_CI_plus_11min=(price_1556-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_11min=. if date>=mdy(4,1,2019)
replace ret_CI_plus_10min=. if ret_CI_plus_11min>0.10 & !missing(ret_CI_plus_11min)
replace ret_CI_plus_11min=. if ret_CI_plus_11min<-0.10 & !missing(ret_CI_plus_11min)
replace ret_CI_plus_11min=ret_CI_plus_11min*100
replace ret_CI_plus_11min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1556))


gen ret_CI_plus_12min=(price_1557-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_12min=. if date>=mdy(4,1,2019)
replace ret_CI_plus_12min=. if ret_CI_plus_12min>0.10 & !missing(ret_CI_plus_12min)
replace ret_CI_plus_12min=. if ret_CI_plus_12min<-0.10 & !missing(ret_CI_plus_12min)
replace ret_CI_plus_12min=ret_CI_plus_12min*100
replace ret_CI_plus_12min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1557))


gen ret_CI_plus_13min=(price_1558-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_13min=. if date>=mdy(4,1,2019)
replace ret_CI_plus_13min=. if ret_CI_plus_13min>0.10 & !missing(ret_CI_plus_13min)
replace ret_CI_plus_13min=. if ret_CI_plus_13min<-0.10 & !missing(ret_CI_plus_13min)
replace ret_CI_plus_13min=ret_CI_plus_13min*100
replace ret_CI_plus_13min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1558))


gen ret_CI_plus_14min=(price_1559-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_14min=. if date>=mdy(4,1,2019)
replace ret_CI_plus_14min=. if ret_CI_plus_14min>0.10 & !missing(ret_CI_plus_14min)
replace ret_CI_plus_14min=. if ret_CI_plus_14min<-0.10 & !missing(ret_CI_plus_14min)
replace ret_CI_plus_14min=ret_CI_plus_14min*100
replace ret_CI_plus_14min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1559))


gen ret_CI_plus_15min=(abs(prc)-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_plus_15min=. if date>=mdy(4,1,2019)
replace ret_CI_plus_15min=. if ret_CI_plus_15min>0.10 & !missing(ret_CI_plus_15min)
replace ret_CI_plus_15min=. if ret_CI_plus_15min<-0.10 & !missing(ret_CI_plus_15min)
replace ret_CI_plus_15min=ret_CI_plus_15min*100
replace ret_CI_plus_15min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(prc))


gen ret_CI_minus_1min=(price_1544-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_minus_1min=(price_1549-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_minus_1min=. if ret_CI_minus_1min>0.10 & !missing(ret_CI_minus_1min)
replace ret_CI_minus_1min=. if ret_CI_minus_1min<-0.10 & !missing(ret_CI_minus_1min)
replace ret_CI_minus_1min=ret_CI_minus_1min*100
replace ret_CI_minus_1min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1544) )
replace ret_CI_minus_1min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1549))


gen ret_CI_minus_2min=(price_1543-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_minus_2min=(price_1548-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_minus_2min=. if ret_CI_minus_2min>0.10 & !missing(ret_CI_minus_2min)
replace ret_CI_minus_2min=. if ret_CI_minus_2min<-0.10 & !missing(ret_CI_minus_2min)
replace ret_CI_minus_2min=ret_CI_minus_2min*100
replace ret_CI_minus_2min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1543) )
replace ret_CI_minus_2min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1548))


gen ret_CI_minus_3min=(price_1542-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_minus_3min=(price_1547-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_minus_3min=. if ret_CI_minus_3min>0.10 & !missing(ret_CI_minus_3min)
replace ret_CI_minus_3min=. if ret_CI_minus_3min<-0.10 & !missing(ret_CI_minus_3min)
replace ret_CI_minus_3min=ret_CI_minus_3min*100
replace ret_CI_minus_3min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1542) )
replace ret_CI_minus_3min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1547))


gen ret_CI_minus_4min=(price_1541-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_minus_4min=(price_1546-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_minus_4min=. if ret_CI_minus_4min>0.10 & !missing(ret_CI_minus_4min)
replace ret_CI_minus_4min=. if ret_CI_minus_4min<-0.10 & !missing(ret_CI_minus_4min)
replace ret_CI_minus_4min=ret_CI_minus_4min*100
replace ret_CI_minus_4min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1541) )
replace ret_CI_minus_4min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1546))


gen ret_CI_minus_5min=(price_1540-price1545)/price1545 if date<mdy(4,1,2019)
replace ret_CI_minus_5min=(price_1545-price_1550)/price_1550 if date>=mdy(4,1,2019)
replace ret_CI_minus_5min=. if ret_CI_minus_5min>0.10 & !missing(ret_CI_minus_5min)
replace ret_CI_minus_5min=. if ret_CI_minus_5min<-0.10 & !missing(ret_CI_minus_5min)
replace ret_CI_minus_5min=ret_CI_minus_5min*100
replace ret_CI_minus_5min=. if  date <mdy(4,1,2019) & (missing(price1545) | missing(price_1540) )
replace ret_CI_minus_5min=. if date >= mdy(4,1,2019) & (missing(price_1550) | missing(price_1545))


replace ret_closing_auction=. if ocount !=1

gen sp500=1 if !missing(sp500_flag)
replace sp500=0 if missing(sp500)


* have lagged volatility
gsort permno date
by permno: gen vol22d_lag1=vol_22d[_n-1]
gen vol_22d_ann=vol22d_lag1*sqrt(252)
replace vol_22d_ann=2 if vol_22d_ann>2
replace vol_22d_ann=0.01 if vol_22d_ann<0.01
* change to lag(Volat) instead of lag(LogVolat)
*gen logvol22d_ann=log(vol_22d_ann)


replace price_lag1=abs(price_lag1)
replace price_lag1=0.5 if price_lag1<0.5
replace price_lag1=300 if price_lag1>300
gen price_measure=-1*log(price_lag1)


gen nasd=1 if exchcd==3
replace nasd=0 if exchcd==1


gen dow = dow(date)
gen EA_dummy1=0 if missing(ea_dummy)
replace EA_dummy1=1 if missing(EA_dummy1)

gen Friday=1 if dow==5
replace Friday=0 if missing(Friday)

gen signed_sqrt_oi=sign(closing_auction_oib)*((abs(closing_auction_oib))^0.5)


replace ret_closing_auction=100*ret_closing_auction


gen yearmonth = 100*year(date)+month(date)
gsort permno yearmonth + date 


set more off 

# ## When NYSE starting time is 15:45 pm 


local return_vars "ret_CI_minus_5min ret_CI_minus_4min ret_CI_minus_3min ret_CI_minus_2min ret_CI_minus_1min ret_CI_plus_1min ret_CI_plus_2min ret_CI_plus_3min ret_CI_plus_4min ret_CI_plus_5min ret_CI_plus_6min ret_CI_plus_7min ret_CI_plus_8min ret_CI_plus_9min ret_CI_plus_10min ret_CI_plus_11min ret_CI_plus_12min ret_CI_plus_13min ret_CI_plus_14min ret_CI_plus_15min"

foreach ret_var of local return_vars {
    preserve 
    keep if date<mdy(10,29,2018)
    * first stage time-series regression for `ret_var'
    *count of observations within permno yearmonth for signed_sqrt_oi value equals to 0 
    bys permno yearmonth: gen count_signed_sqrt_oi_0 = sum(signed_sqrt_oi == 0)
    bys permno yearmonth: replace count_signed_sqrt_oi_0 = count_signed_sqrt_oi_0[_N]

    bys permno yearmonth: asreg `ret_var' signed_sqrt_oi
    bys permno yearmonth: egen sum_signed_sqrt_oi=total(signed_sqrt_oi)
    bys permno yearmonth: egen sum_rets=total(`ret_var')

    * filters in place 
    bys permno yearmonth: keep if missing(_R2)==0
    bys permno yearmonth: keep if _Nobs>10
    bys permno yearmonth: replace _b_signed_sqrt_oi=. if count_signed_sqrt_oi_0>15
    bys permno yearmonth: drop if sum_signed_sqrt_oi==0
    bys permno yearmonth: drop if sum_rets==0
    bys permno yearmonth: keep if _n==1

    gen volatile=vol_22d_ann
    gen inverseprice=price_measure

    gen dummylarge_nasd=DummyLarge*nasd
    gen dummysmall_nasd=DummySmall*nasd
    gen dummymicro_nasd=DummyMicro*nasd

    gen dummylarge_volatile=DummyLarge*vol_22d_ann
    gen dummysmall_volatile=DummySmall*vol_22d_ann
    gen dummymicro_volatile=DummyMicro*vol_22d_ann

    gen dummylarge_inverseprice=DummyLarge*price_measure
    gen dummysmall_inverseprice=DummySmall*price_measure
    gen dummymicro_inverseprice=DummyMicro*price_measure
    
    winsor2 _b_signed_sqrt_oi, cuts(1  99)
    xtset permno yearmonth
    drop _Nobs _R2 _adjR2 _b_cons
    
    eststo clear 
    eststo: asreg _b_signed_sqrt_oi_w DummyMicro DummySmall DummyLarge , noconstant fmb first newey(12)
    eststo: asreg _b_signed_sqrt_oi_w DummyMicro DummySmall DummyLarge dummymicro_nasd  dummysmall_nasd dummylarge_nasd, noconstant fmb first newey(12) save(PanelA_`ret_var')

    esttab  using `"${output}PI_subperiodPanelA.csv"', scalars("N N" "r2_a Adj.R-sq") sfmt(%-8.0f %-8.3f) title("Before Oct 29 2018: Per minute `ret_var' ") append indicate(`r(indicate_fe)') starlevels(* 0.10 ** 0.05 *** 0.01)  nogaps
    restore
}

# %%
restore 

# %%
local return_vars "ret_CI_minus_5min ret_CI_minus_4min ret_CI_minus_3min ret_CI_minus_2min ret_CI_minus_1min ret_CI_plus_1min ret_CI_plus_2min ret_CI_plus_3min ret_CI_plus_4min ret_CI_plus_5min ret_CI_plus_6min ret_CI_plus_7min ret_CI_plus_8min ret_CI_plus_9min ret_CI_plus_10min ret_CI_plus_11min ret_CI_plus_12min ret_CI_plus_13min ret_CI_plus_14min ret_CI_plus_15min"

foreach ret_var of local return_vars {
    preserve 
    keep if date>=mdy(10,29,2018) & date<mdy(4,1,2019)
    * first stage time-series regression for `ret_var'
    *count of observations within permno yearmonth for signed_sqrt_oi value equals to 0 
    bys permno yearmonth: gen count_signed_sqrt_oi_0 = sum(signed_sqrt_oi == 0)
    bys permno yearmonth: replace count_signed_sqrt_oi_0 = count_signed_sqrt_oi_0[_N]

    bys permno yearmonth: asreg `ret_var' signed_sqrt_oi
    bys permno yearmonth: egen sum_signed_sqrt_oi=total(signed_sqrt_oi)
    bys permno yearmonth: egen sum_rets=total(`ret_var')

    * filters in place 
    bys permno yearmonth: keep if missing(_R2)==0
    bys permno yearmonth: keep if _Nobs>10
    bys permno yearmonth: replace _b_signed_sqrt_oi=. if count_signed_sqrt_oi_0>15
    bys permno yearmonth: drop if sum_signed_sqrt_oi==0
    bys permno yearmonth: drop if sum_rets==0
    bys permno yearmonth: keep if _n==1

    gen volatile=vol_22d_ann
    gen inverseprice=price_measure

    gen dummylarge_nasd=DummyLarge*nasd
    gen dummysmall_nasd=DummySmall*nasd
    gen dummymicro_nasd=DummyMicro*nasd

    gen dummylarge_volatile=DummyLarge*vol_22d_ann
    gen dummysmall_volatile=DummySmall*vol_22d_ann
    gen dummymicro_volatile=DummyMicro*vol_22d_ann

    gen dummylarge_inverseprice=DummyLarge*price_measure
    gen dummysmall_inverseprice=DummySmall*price_measure
    gen dummymicro_inverseprice=DummyMicro*price_measure
    
    winsor2 _b_signed_sqrt_oi, cuts(1  99)
    xtset permno yearmonth
    drop _Nobs _R2 _adjR2 _b_cons
    
    eststo clear 
    eststo: asreg _b_signed_sqrt_oi_w DummyMicro DummySmall DummyLarge , noconstant fmb first 
    eststo: asreg _b_signed_sqrt_oi_w DummyMicro DummySmall DummyLarge dummymicro_nasd  dummysmall_nasd dummylarge_nasd, noconstant fmb first save(PanelB_`ret_var')

    esttab  using `"${output}PI_subperiodPanelB.csv"', scalars("N N" "r2_a Adj.R-sq") sfmt(%-8.0f %-8.3f) title("Oct 2018 to April 2019: Per minute `ret_var' ") append indicate(`r(indicate_fe)') starlevels(* 0.10 ** 0.05 *** 0.01)  nogaps
    restore
}


local return_vars "ret_CI_minus_5min ret_CI_minus_4min ret_CI_minus_3min ret_CI_minus_2min ret_CI_minus_1min ret_CI_plus_1min ret_CI_plus_2min ret_CI_plus_3min ret_CI_plus_4min ret_CI_plus_5min ret_CI_plus_6min ret_CI_plus_7min ret_CI_plus_8min ret_CI_plus_9min ret_CI_plus_10min"

foreach ret_var of local return_vars {
    preserve 
    keep if date>=mdy(4,1,2019)
    * first stage time-series regression for `ret_var'
    *count of observations within permno yearmonth for signed_sqrt_oi value equals to 0 
    bys permno yearmonth: gen count_signed_sqrt_oi_0 = sum(signed_sqrt_oi == 0)
    bys permno yearmonth: replace count_signed_sqrt_oi_0 = count_signed_sqrt_oi_0[_N]

    bys permno yearmonth: asreg `ret_var' signed_sqrt_oi
    bys permno yearmonth: egen sum_signed_sqrt_oi=total(signed_sqrt_oi)
    bys permno yearmonth: egen sum_rets=total(`ret_var')

    * filters in place 
    bys permno yearmonth: keep if missing(_R2)==0
    bys permno yearmonth: keep if _Nobs>10
    bys permno yearmonth: replace _b_signed_sqrt_oi=. if count_signed_sqrt_oi_0>15
    bys permno yearmonth: drop if sum_signed_sqrt_oi==0
    bys permno yearmonth: drop if sum_rets==0
    bys permno yearmonth: keep if _n==1

    gen volatile=vol_22d_ann
    gen inverseprice=price_measure

    gen dummylarge_nasd=DummyLarge*nasd
    gen dummysmall_nasd=DummySmall*nasd
    gen dummymicro_nasd=DummyMicro*nasd

    gen dummylarge_volatile=DummyLarge*vol_22d_ann
    gen dummysmall_volatile=DummySmall*vol_22d_ann
    gen dummymicro_volatile=DummyMicro*vol_22d_ann

    gen dummylarge_inverseprice=DummyLarge*price_measure
    gen dummysmall_inverseprice=DummySmall*price_measure
    gen dummymicro_inverseprice=DummyMicro*price_measure
    
    winsor2 _b_signed_sqrt_oi, cuts(1  99)
    xtset permno yearmonth
    drop _Nobs _R2 _adjR2 _b_cons
    
    eststo clear 
    eststo: asreg _b_signed_sqrt_oi_w DummyMicro DummySmall DummyLarge , noconstant fmb first newey(12)
    eststo: asreg _b_signed_sqrt_oi_w DummyMicro DummySmall DummyLarge dummymicro_nasd  dummysmall_nasd dummylarge_nasd, noconstant fmb first save(PanelC_`ret_var') newey(12)

    esttab  using `"${output}PI_subperiodPanelC.csv"', scalars("N N" "r2_a Adj.R-sq") sfmt(%-8.0f %-8.3f) title("Post 2019: Per minute `ret_var' ") append indicate(`r(indicate_fe)') starlevels(* 0.10 ** 0.05 *** 0.01)  nogaps
    restore
}
